<?php
/**
 * Created by PhpStorm.
 * User: jiaer
 * Date: 2019/4/25
 * Time: 16:10
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;

class User extends Controller
{
    /**
     * 主页面
     * User: jex Date: 2019/4/19 10:03
     */
    public function index()
    {
        return view();
    }

    /**
     * 详情页面
     * User: jex Date: 2019/4/19 16:29
     */
    public function info()
    {
        return view();
    }

    /**
     * 主数据集合
     * User: jex Date: 2019/4/19 15:20
     */
    public function data_list()
    {

        $department_id = intval(input("department_id"));
        $position_id = intval(input("position_id"));
        $role_id = intval(input("role_id"));
        $status = input("status");
        $key = input("key");

        $map = array();
        if ($department_id > 0) {
            $map['a.department_id'] = $department_id;
        }
        if ($position_id > 0) {
            $map['a.position_id'] = $position_id;
        }
        if ($role_id > 0) {
            $map['a.role_id'] = $role_id;
        }
        if (!empty($status)) {
            $map['a.status'] = $status;
        }
        if (!empty($key)) {
            $map['a.account | a.name'] = ['like', '%' . trim($key) . '%'];
        }

        $map['a.is_delete'] = 0;
        //分页，排序
        $offset = input("offset/d", 0);
        $limit = input("limit/d", 999999999);
        $sort = input("sort", "create_date");
        $order = input("order", "DESC");
        $count = Db::name('s_user')->alias('a')->where($map)->count();
        $data = Db::name('s_user')
            ->alias('a')
            ->join('s_department d','a.department_id = d.id and a.is_delete = d.is_delete', 'left')
            ->join('s_position p','a.position_id = p.id and a.is_delete = p.is_delete', 'left')
            ->join('s_role r','a.role_id = r.id and a.is_delete = r.is_delete', 'left')
            ->where($map)
            ->order($sort . ' ' . $order)
            ->limit($offset, $limit)
            ->field('a.id,a.name,a.account,a.status,d.name as department_name,p.name as position_name,r.name as role_name,a.sex,a.age,a.mobile,a.mail,a.remark,a.create_user,a.create_time,a.last_login_date,a.last_login_ip')
            ->select();

        return table_msg($data, $count);
    }

    /**
     * 数据详情
     * User: jex Date: 2019/4/19 16:26
     */
    public function info_data()
    {
        $id = intval(input('id'));
        if (empty($id)) {
            return error_msg('ID接收异常');
        }
        $data = Db::name('s_user')
            ->where(['id' => $id, 'is_delete' => 0])
            ->field('id,name,account,status,department_id,position_id,role_id,sex,age,mobile,mail,remark')
            ->find();

        return info_msg($data);
    }

    /**
     * 保存/新增 详情数据
     * User: jex Date: 2019/4/19 16:30
     */
    public function post_data()
    {
        $id = intval(input('id'));
        $name = input('name');
        $account = input('account');
        $status = input('status');
        $department_id = intval(input('department_id'));
        $position_id = intval(input('position_id'));
        $role_id = intval(input('role_id'));

        $mobile = input('mobile');
        $mail = input('mail');
        $remark = input('remark');

        $password = input('password');
        //数据验证
        if (empty($name)) {
            return error_msg('名称为空');
        }
        if (empty($account)) {
            return error_msg('账户为空');
        }
        if($status != 0)
        {
            if (empty($status)) {
                return error_msg('请选择状态');
            }
        }
        if (empty($department_id)) {
            return error_msg('请选择部门');
        }
        if (empty($position_id)) {
            return error_msg('请选择职位');
        }
        if (empty($role_id)) {
            return error_msg('请选择角色');
        }


        $user = get_user();
        $current_time = get_date_time();
        $data = [];
        $data['name'] = $name;
        $data['account'] = $account;
        $data['status'] = $status;
        $data['department_id'] = $department_id;
        $data['position_id'] = $position_id;
        $data['role_id'] = $role_id;
        $data['mobile'] = $mobile;
        $data['mail'] = $mail;
        $data['remark'] = $remark;
        if(!empty($password)){
            $data['password'] = $password;
        }
        if (empty($id)) {
            //数据重复验证
            $tab_data_count = DB::name('s_user')
                ->where(['account' => $account, 'is_delete' => 0])
                ->count();

            if ($tab_data_count > 0) {
                return error_msg('当前账号已存在');
            }
            if(!isset($data['password'])){
                return error_msg('请填写密码');
            }
            $data['create_user_id'] = $user['id'];
            $data['create_user'] = $user['name'];
            $data['create_time'] = $current_time;
            $data['update_user_id'] = $user['id'];
            $data['update_user'] = $user['name'];
            $data['update_time'] = $current_time;
            $data['is_delete'] = 0;
            $id = DB::name('s_user')->insertGetId($data);
            //记录日志
            wlog('用户管理', "新增成功，用户名：$account", $id);
        } else {
            //数据重复验证
            $tab_data_count = DB::name('s_user')
                ->where(['id' => ['<>', $id], 'account' => $account, 'is_delete' => 0])
                ->count();

            if ($tab_data_count > 0) {
                return error_msg('当前账号已存在');
            }
            $data['update_user_id'] = $user['id'];
            $data['update_user'] = $user['name'];
            $data['update_time'] = $current_time;
            DB::name('s_user')->where(['id' => $id])->update($data);

            //记录日志
            wlog('用户管理', "编辑成功，用户名：$account", $id);
        }

        return success_msg();
    }

    /**
     * 单据假删除
     * User: jex Date: 2019/4/24 11:05
     */
    public function delete()
    {
        $ids = input('ids/a');
        //数据验证
        if (empty($ids)) {
            return error_msg('ID接收异常');
        }
        DB::name('s_user')
            ->where(['id' => ['in', $ids], 'is_delete' => 0])
            ->update(['is_delete' => 1]);
        //记录日志
        wlog('用户管理', "删除成功", $ids);
        return success_msg();
    }


    #region 功能权限

    public function funcpower()
    {
        return view();
    }

    public function menu_button_lst3()
    {
        $user_id = input('user_id/d', 0);
        $map = array();
        $map['user_id'] = $user_id;
        $data = DB::name('s_user_menu_button')->where($map)->field('menu_id,button_id')->select();
        return json($data);
    }

    public function menu_button_save()
    {
        $user_id = input('user_id/d', 0);
        $data = input('data/a');

        Db::startTrans();
        DB::name('s_user_menu_button')
            ->where(['user_id'=>$user_id])
            ->delete();
        if (!empty($data)) {
            foreach ($data as $v) {
                $data = array();
                $data['user_id'] = $user_id;
                $data['menu_id'] = $v['menu_id'];
                $data['button_id'] = $v['button_id'];
                DB::name('s_user_menu_button')->insert($data);
            }
        }
        Db::commit();

        return success_msg();
    }

    #endregion

    /**
     * 用户禁用
     * User: ErXiao Date: 2019/8/16 9:39
     */
    public function disable()
    {
        $id = intval(input("id"));

        //数据验证
        if (empty($id)) {
            return error_msg('ID接收异常');
        }
        // 设置用户为禁用状态
        DB::name("s_user")->where(array("id" => $id))->update(['status' => '禁用']);
        wlog("用户管理", "用户禁用成功");

        return success_msg();
    }

    public function enable()
    {
        $id = intval(input("id"));  

        //数据验证
        if (empty($id)) {
            return error_msg('ID接收异常');
        }
        DB::name("s_user")->where(array("id" => $id))->update(['status' => '正常']);
        wlog("用户管理", "用户启用成功");

        return success_msg();
    }

    /**
     * 一键白班
     * User: Funk Date: 2019/8/28 17:00
     */
    public function white(){
        $ids = input('ids/a');
        //数据验证
        if (empty($ids)) {
            return error_msg('ID接收异常');
        }
        $user = get_user();
        $current_time = get_date_time();
        $data['shift'] = '白班';
        $data['last_update_user_id'] = $user['id'];
        $data['last_update_user_name'] = $user['name'];
        $data['last_update_date'] = $current_time;
        DB::name('s_user')
            ->where(['id' => ['in', $ids], 'is_delete' => 0])
            ->update($data);
        //记录日志
        wlog('用户管理', "班次设置为白班成功", $ids);
        return success_msg();
    }

    /**
     * 一键白班
     * User: Funk Date: 2019/8/28 17:30
     */
    public function black(){
        $ids = input('ids/a');
        //数据验证
        if (empty($ids)) {
            return error_msg('ID接收异常');
        }
        $user = get_user();
        $current_time = get_date_time();
        $data['shift'] = '晚班';
        $data['update_user_id'] = $user['id'];
        $data['update_user'] = $user['name'];
        $data['update_time'] = $current_time;
        DB::name('s_user')
            ->where(['id' => ['in', $ids], 'is_delete' => 0])
            ->update($data);
        //记录日志
        wlog('用户管理', "班次设置为晚班成功", $ids);
        return success_msg();
    }

    public function wx_user()
    {
        $key = input('key','');
        if(!empty($key)){
            $where['nickName|phone']=['like','%'.$key.'%'];
        }
        $company_list = DB::name('a_user')
            ->where($where)
            ->field('phone,nickName,openid,id')
            ->select();
        return json($company_list);
    }
}